package com.runfast.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.Enumeration;

public class LogUtils {

    public static final Logger LOGGER = LoggerFactory.getLogger(LogUtils.class);

    public static void info(HttpServletRequest request) {
        LOGGER.info("=====================request begin==========================");
        String uri = request.getRequestURI();
        String queryString = request.getQueryString();
        if (StringUtils.isNotBlank(queryString)) {
            uri = uri + "?" + queryString;
        }
        LOGGER.info("{}:{}", request.getMethod(), uri);
        LOGGER.info("remote ip:{}  sessionId:{}  ", IpUtils.getIpAddr(request), request.getRequestedSessionId());
        LOGGER.info("===header begin============================================");
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            String value = headersToString(request.getHeaders(name));
            LOGGER.info("{}={}", name, value);
        }
        LOGGER.info("===header   end============================================");
        LOGGER.info("===parameter begin==========================================");
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            String value = StringUtils.join(request.getParameterValues(name), "||");
            LOGGER.info("{}={}", name, value);
        }
        LOGGER.info("===parameter   end==========================================");
        LOGGER.info("=====================request   end==========================");
    }

    private static String headersToString(Enumeration<String> headers) {
        StringBuilder s = new StringBuilder();
        int index = 0;
        while (headers.hasMoreElements()) {
            if (index > 0) {
                s.append("||");
            }
            s.append(headers.nextElement());
        }
        return s.toString();
    }

    public static void info(HttpServletResponse response) {
        LOGGER.info("=====================response begin==========================");
        LOGGER.info("status:{}", response.getStatus(), response.getContentType());
        LOGGER.info("contentType:{}, characterEncoding:{}", response.getContentType(), response.getCharacterEncoding());
        LOGGER.info("===header begin============================================");
        Collection<String> headerNames = response.getHeaderNames();
        for (String name : headerNames) {
            String value = StringUtils.join(response.getHeaders(name), "||");
            LOGGER.info("{}={}", name, value);
        }
        LOGGER.info("===header   end============================================");
        LOGGER.info("=====================response   end==========================");
    }

}
